/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* lastNode = nullptr;
    int minimumDifference = INT_MAX;

    int getMinimumDifference(TreeNode* root) {
        getMinimum(root);
        return minimumDifference;
    }

    void getMinimum(TreeNode* root) {
        if(root == nullptr) return;

        getMinimum(root->left);

        if(lastNode != nullptr) minimumDifference = min(minimumDifference, root->val - lastNode->val);

        lastNode = root;

        getMinimum(root->right);
    }
};